package com.zx.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.zx.wx.utils.BaseUtils;
import com.zx.wx.utils.MenuUtils;
import com.zx.wx.utils.OAuthUtils;
import com.zx.wx.wxInterface.Base;
import com.zx.wx.wxInterface.Request;



@Controller
@RequestMapping(value = "/wx")
public class wx {
	@Autowired 
	private Base base;
	@Autowired
	private Request Request;
	
	
	@ResponseBody
	@RequestMapping(value = "/wxconfig")							
	public Map<String,String> wxconfig(@RequestParam(defaultValue="wxc408c0125834275f")String appID,@RequestParam(defaultValue="d0dee76c226c661336a7a9baf4707166") String appsecret,String url) throws UnsupportedEncodingException {
//		String encodeUrl=URLEncoder.encode(url, "UTF-8");
		Map<String, String> config=BaseUtils.getConfig(appID,appsecret,url,base,Request);
		System.out.println("config:"+config);
		return config;
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = "/accessToken")
	public Map<String,String> accessToken(@RequestParam(defaultValue="wxc408c0125834275f")String appID,@RequestParam(defaultValue="d0dee76c226c661336a7a9baf4707166") String appsecret) {
 		Map<String,String> result=new HashMap<>();
		String access_token=BaseUtils.getAccess_token(appID, appsecret,base,Request);
		result.put("access_token",access_token);
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/createMenu")
	public String createMenu(@RequestParam(defaultValue="wxc408c0125834275f")String appID,@RequestParam(defaultValue="d0dee76c226c661336a7a9baf4707166") String appsecret){
		String access_token=BaseUtils.getAccess_token(appID, appsecret,base,Request);
		String result=MenuUtils.createMenu(access_token,"sub_button",Request);
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/selectMenu")
	public String selectMenu(@RequestParam(defaultValue="wxc408c0125834275f")String appID,@RequestParam(defaultValue="d0dee76c226c661336a7a9baf4707166") String appsecret) throws UnsupportedEncodingException {
		String access_token=BaseUtils.getAccess_token(appID, appsecret,base,Request);
		String result=MenuUtils.selectMenu(access_token,Request);
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/deletetMenu")
	public String deletetMenu(@RequestParam(defaultValue="wxc408c0125834275f")String appID,@RequestParam(defaultValue="d0dee76c226c661336a7a9baf4707166") String appsecret) {
		String access_token=BaseUtils.getAccess_token(appID, appsecret,base,Request);
		String result=MenuUtils.deleteMenu(access_token,Request);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/getOpenid")
	public Map<String,String> getOpenid(@RequestParam(defaultValue="wxc408c0125834275f")String appID,@RequestParam(defaultValue="d0dee76c226c661336a7a9baf4707166") String appsecret,String code) {
		Map<String,String> result=OAuthUtils.codeToOpenId(appID, appsecret, code, Request);
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/getUserInfo")
	public Map<String, String> getUserInfo(@RequestParam(defaultValue="wxc408c0125834275f")String appID,@RequestParam(defaultValue="d0dee76c226c661336a7a9baf4707166") String appsecret,String access_token,String openid) {
//		zh_CN 简体，zh_TW 繁体，en 英语
		Map<String, String> result=OAuthUtils.openIdToUserInfo(access_token, openid, "zh_CN", Request);
		return result;
	}
	public String  getFile(MultipartFile[] file) throws IOException{
		for(MultipartFile item:file){
			@SuppressWarnings("unused")
			InputStream files=item.getInputStream();//获取文件流
			item.getOriginalFilename();//获取文件名
		}
		
		return null;
	}
}
